import 'package:extended_nested_scroll_view/extended_nested_scroll_view.dart';
import 'package:flutter/material.dart';
import 'package:xws/utils/theme_util.dart';

import 'friends_pub.dart';
import 'home_banner.dart';
import 'home_tab.dart';
import 'near_list.dart';

/// 主页
class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  late NearListController _nearListController;
  late FriendsPostController _friendsPostController;

  @override
  void initState() {
    _nearListController = NearListController();
    _friendsPostController = FriendsPostController();
    super.initState();
  }

  @override
  void dispose() {
    _nearListController.dispose();
    _friendsPostController.dispose();
    super.dispose();
  }

  /// 筛选条件改变回调
  Future<void> _onFilterChanged(int tabIndex, int value) async {
    if (tabIndex == 0) {
      await _nearListController.changeFilter(value);
    } else {
      await _friendsPostController.changeFilter(value);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(bottom: kToolbarHeight),
      child: DefaultTabController(
        length: 2,
        child: ExtendedNestedScrollView(
          floatHeaderSlivers: true,
          onlyOneScrollInBody: true,
          pinnedHeaderSliverHeightBuilder: () => kToolbarHeight + statusBarHeight,
          headerSliverBuilder: (_, __) {
            return <Widget>[
              const HomeBanner(),
              HomeTab(onChanged: _onFilterChanged),
            ];
          },
          body: TabBarView(
            physics: const BouncingScrollPhysics(),
            children: <Widget>[
              NearList(controller: _nearListController),
              FriendsPub(controller: _friendsPostController),
            ],
          ),
        ),
      ),
    );
  }
}
